home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Amiga Plus 1.adf / Programming / SampleMenu.c < prev    next >
C/C++ Source or Header  |  1978-04-08  |  6KB  |  207 lines

  1. /* :ts=8 */
  2. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  3. /*                                                              */
  4. /*      SampleMenu.c                                            */
  5. /*                                                              */
  6. /*      Menu builder for AmigaPLUS article about Intuition     */
  7. /*      Written by Michael G. Lehman                            */
  8. /*      of Intuitive Technologies                               */
  9. /*                                                              */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  11.  
  12.  
  13. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  14. /*      Complete Include file for Amiga programs                */
  15. /*      Derived from examples supplied by Commodore Amiga       */
  16. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
  17.  
  18.  
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <exec/memory.h>
  23. #include <exec/ports.h>
  24. #include <exec/tasks.h>
  25. #include <exec/libraries.h>
  26. #include <exec/devices.h>
  27. #include <exec/io.h>
  28. #include <exec/devices.h>
  29.  
  30. #include <libraries/dos.h>
  31.  
  32. #include <graphics/gfx.h> /* ALWAYS INCLUDE GFX.H before other includes */
  33. #include <graphics/display.h>
  34. #include <graphics/clip.h>
  35. #include <graphics/rastport.h>
  36. #include <graphics/gfxbase.h>
  37. #include <graphics/text.h>
  38. #include <graphics/regions.h>
  39. #include <graphics/copper.h>
  40. #include <graphics/gels.h>
  41.  
  42. #include <devices/inputevent.h>
  43. #include <devices/gameport.h>
  44. #include <devices/console.h>
  45. #include <devices/keymap.h>
  46.  
  47. #include <intuition/intuition.h>
  48.  
  49.  
  50. #define PUBCLEAR MEMF_PUBLIC | MEMF_CLEAR
  51.  
  52. struct Menu *NewMenu();
  53. struct MenuItem *AddItem();
  54. struct MenuItem *AddSubItem();
  55.  
  56. struct Window *Mwindow; /* currently chosen window and screen for */
  57. struct Screen *Mscreen; /* this menu build operation              */
  58.  
  59.  
  60. #define ITEMSTD         ITEMTEXT | ITEMENABLED | HIGHCOMP
  61. #define ITEMSTDBOX      ITEMTEXT | ITEMENABLED | HIGHBOX
  62. #define CHKITEMSTDBOX   CHECKIT | ITEMSTDBOX
  63. #define CTLITEMSTD      (w != controlwindow ? ITEMSTD : ITEMTEXT | HIGHCOMP)
  64. #define OFFITEMSTD      ITEMTEXT | HIGHCOMP
  65. #define CHKITEMSTD      ITEMSTD | CHECKIT
  66. #define OFFCHKITEMSTD   OFFITEMSTD | CHECKIT
  67.  
  68. struct Menu *MMenu;
  69. struct MenuItem *NMenu,*OMenu;
  70. extern struct GfxBase *GfxBase;
  71.  
  72. /*****************/
  73. /* MENU FUNCTIONS*/
  74. /*****************/
  75.  
  76. int     Draw1();
  77. int     Draw2();
  78. int     Quit();
  79.  
  80.  
  81. struct Menu *InitMenu(s,w)      /* returns pointer to root of list */
  82. struct Screen *s;    /* pointer to the screen, used for TextLength calls */
  83. struct Window *w;    /* pointer to the window for which to build menu */
  84. {
  85.  
  86.   register struct Menu *mp,*rmp;/* current menu pointer, root menu pointer */
  87.   register struct MenuItem *mip,*mip2;
  88.  
  89.  
  90.         Mscreen = s;
  91.         Mwindow = w;
  92.  
  93.         rmp = NULL;
  94.  
  95.         mp = NewMenu(   rmp,
  96.                         -1,0,    /* origin */
  97.                         -1,10,  /* width (-1 means figure it out for me), */
  98.                                 /* height */
  99.                         MENUENABLED,
  100.                         "Test Menu");
  101.  
  102.         rmp = mp;
  103.  
  104.         AddItem(mp,ITEMSTD,"Draw String 1",Draw1);
  105.         AddItem(mp,ITEMSTD,"Draw String 2",Draw2);
  106.         AddItem(mp,ITEMSTD | COMMSEQ,"Quit",Quit,'Q');
  107.  
  108.      /*====================================================================*/
  109.      /* this section of code scans the entire menu tree,                   */
  110.      /* it looks for submenus and passes them to be positioned optimumlly. */
  111.      /*====================================================================*/
  112.  
  113.       MMenu = rmp;
  114.       while ( MMenu )
  115.       {
  116.         NMenu = MMenu -> FirstItem;
  117.         while ( NMenu )
  118.         {
  119.           if ( NMenu -> SubItem)
  120.             PositionSubMenu(MMenu,NMenu);
  121.           NMenu = NMenu -> NextItem;
  122.         }
  123.         MMenu = MMenu -> NextMenu;
  124.       }
  125.  
  126.   return(rmp);
  127. }
  128.  
  129. PositionSubMenu(Menu,smenu)
  130.   struct Menu *Menu;
  131.   struct MenuItem *smenu;
  132. {
  133.   register bottom_edge=0,right_edge=0,top_edge=0xFFFF,left_edge=0xFFFF;
  134.   int most = 0,ypos,edge;
  135.   struct MenuItem *temp,*menu;
  136.   struct IntuiText *ITText;
  137.  
  138.   ypos = smenu -> TopEdge;
  139.  
  140.   smenu = smenu -> SubItem;
  141.   menu = Menu -> FirstItem;
  142.  
  143.   temp = smenu;
  144.   while ( temp )
  145.   {
  146.     if ( temp -> LeftEdge < left_edge )
  147.       left_edge = temp -> LeftEdge;
  148.     if ( temp -> TopEdge  < top_edge )
  149.       top_edge  = temp -> TopEdge;
  150.     if ( temp -> LeftEdge + temp -> Width > right_edge )
  151.       right_edge = temp -> LeftEdge + temp -> Width;
  152.     if ( temp -> TopEdge + temp -> Height > bottom_edge )
  153.       bottom_edge = temp -> TopEdge + temp -> Height;
  154.     temp = temp -> NextItem;
  155.   }
  156.   /* we now have the outside coordinates of the submenu box */
  157.   top_edge += ypos;
  158.   bottom_edge += ypos;
  159.  
  160.   temp = menu;
  161.   while ( temp )
  162.   {
  163.     /* if this conditional is true then this menu item is adjacent to the
  164.        submenu box */
  165.     if ( temp -> TopEdge > top_edge - 8 &&
  166.          temp -> TopEdge + temp -> Height < bottom_edge + 8 )
  167.     {
  168.       edge = temp -> LeftEdge + temp -> Width;
  169.       ITText = temp -> ItemFill;
  170.       if ( strlen ( ITText -> IText ) > most ) most = strlen ( ITText -> IText);
  171.     }
  172.     temp = temp -> NextItem;
  173.   }
  174.   most = left_edge - ( most * 8 ) - 4 ;
  175.   if ( right_edge - most < edge ) most -= edge - ( right_edge - most );
  176.   /* now we now the longest menu item that is adjacent to the submenu box */
  177.  
  178.   temp = smenu;
  179.   while ( temp )
  180.   {
  181.     temp -> LeftEdge -= most;
  182.     temp = temp -> NextItem;
  183.   }
  184. }
  185.  
  186. Quit()
  187. {
  188.   extern SHORT quitcode;
  189.  
  190.   quitcode = TRUE;
  191. }
  192.  
  193. Draw1()
  194. {
  195.   extern struct Window *window;
  196.   Move(window -> RPort,50,50);
  197.   Text(window -> RPort,"AmigaPLUS Programming example",29);
  198. }
  199.  
  200. Draw2()
  201. {
  202.   extern struct Window *window;
  203.   Move(window -> RPort,50,90);
  204.   Text(window -> RPort,"Intuition Plus",14);
  205. }
  206.  
  207.